﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace asscope.core.Services
{
    public class Country
    {

        private static Entities.Country CreateEmptyEntities()
        {
            var country = new Entities.Country
            {
                id = string.Empty,
                code = string.Empty,
                name = string.Empty
            };
            return country;
        }

        public static Entities.Country ConvertToEntity(DataRow dr)
        {
            var country = new Entities.Country
            {
                id = (dr["id"] != DBNull.Value) ? dr["id"].ToString() : string.Empty,
                code = (dr["code"] != DBNull.Value) ? dr["code"].ToString() : string.Empty,
                name = (dr["name"] != DBNull.Value) ? dr["name"].ToString() : string.Empty
                /*
                BranchID = (dr["ID"] != DBNull.Value) ? dr["ID"].ToString() : string.Empty,
                CentralID = (dr["CentralID"] != DBNull.Value) ? dr["CentralID"].ToString() : string.Empty,
                CentralName = (dr["CentralName"] != DBNull.Value) ? dr["CentralName"].ToString() : string.Empty,
                BranchName = (dr["BranchName"] != DBNull.Value) ? dr["BranchName"].ToString() : string.Empty,
                BranchNameEng = (dr["BranchNameEng"] != DBNull.Value) ? dr["BranchNameEng"].ToString() : string.Empty,
                ShortCode = (dr["ShortCode"] != DBNull.Value) ? dr["ShortCode"].ToString() : string.Empty,
                Address = (dr["Address"] != DBNull.Value) ? dr["Address"].ToString() : string.Empty,
                AddressEng = (dr["AddressEng"] != DBNull.Value) ? dr["AddressEng"].ToString() : string.Empty,
                MapUrl = (dr["MapUrl"] != DBNull.Value) ? dr["MapUrl"].ToString() : string.Empty
                 * */
            };
            return country;
        }


        public Entities.Country[] SelectAll()
        {
            var cmd = new SqlCommand
            {
                CommandType = CommandType.Text,
                CommandText = "select * from country",
                CommandTimeout = 0
            };

            //var dt =   SQLHelper.ExecuteDataset(cmd).Tables[0];


            asscope.Data.Layer.Country Countries = new asscope.Data.Layer.Country();

            DataSet ds = Countries.SelectAll();

            DataTable dt = ds.Tables[0];



            if (dt.Rows.Count > 0)
            {
                var Countrys = new Entities.Country[dt.Rows.Count];
                var i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    Countrys[i] = ConvertToEntity(dr);
                    i++;
                }
                return Countrys;
            }
            var country = new Entities.Country[1];
            country[0] = CreateEmptyEntities();
            return country;
        }




    }
}
